package cn.edu.neusoft.demo.hospital2.Service.impl;

import cn.edu.neusoft.demo.hospital2.Dao.DrugDao;
import cn.edu.neusoft.demo.hospital2.Service.DrugService;
import cn.edu.neusoft.demo.hospital2.pojo.Drug;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DrugServiceImpl implements DrugService {
    @Autowired
    private DrugDao drugDao;

    @Override
    public List<Drug> getAllDrugs() {
        return drugDao.getAllDrugs();
    }

    @Override
    public Drug getDrugById(Integer drugId) {
        return drugDao.getDrugById(drugId);
    }

    @Override
    public int insertDrug(Drug drug) {
        return drugDao.insertDrug(drug);
    }

    @Override
    public int updateDrug(Drug drug) {
        return drugDao.updateDrug(drug);
    }

    @Override
    public int deleteDrug(Integer drugId) {
        return drugDao.deleteDrug(drugId);
    }

    @Override
    public int copyDrug(Integer drugId) {
        Drug originalDrug = drugDao.getDrugById(drugId);
        if (originalDrug != null) {
            Drug newDrug = new Drug();
            BeanUtils.copyProperties(originalDrug, newDrug);
            newDrug.setDrugId(null); // 清空ID，以便插入新记录
            return drugDao.insertDrug(newDrug);
        }
        return 0;
    }
}